package com.zhixiang.wbl.visitor.fragement;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.zhixiang.wbl.visitor.R;
import com.zhixiang.wbl.visitor.Utils.LocationUtil;
import com.zhixiang.wbl.visitor.Utils.VisitorUtil;
import com.zhixiang.wbl.visitor.bean.CustomMaker;
import com.zhixiang.wbl.visitor.imageLoader.AsyncImageLoader;
import com.zhixiang.wbl.visitor.model.location.LocationModel;
import com.zhixiang.wbl.visitor.model.location.RootLocation;
import com.zhixiang.wbl.visitor.model.login.Visitor;

/**
 * Created by dongzhixiang on 16/5/17.
 */
public class DiscoverFragment extends BaseFragment
{
    private static final String TAG = "DiscoverFragment";
    View mainView = null;
    MapView mMapView = null;
    BaiduMap mBaiduMap = null;
    Visitor mVisitor = null;
    LocationUtil locationUtil = null;
    //Map<Integer,>
    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mainView =    inflater.inflate(R.layout.fragment_discover,container,false);
        return mainView;
    }

    @Override
    public void init()
    {
        //更新当前位置
        mVisitor = VisitorUtil.getCurrentVisitor(getContext());
        //获取地图控件引用
        mMapView = (MapView) mainView.findViewById(R.id.bmapView1);
        mBaiduMap = mMapView.getMap();
        //先从缓存中获取女用户的位置信息
        LocationModel loc = VisitorUtil.getLocation(getContext());
        if (loc!=null)
        {
            setCenterLocationAndAddMaker(loc);
        }
        else
        {

            if (mVisitor!=null)
            {
                locationUtil = new LocationUtil(mVisitor.getId(), new LocationUtil.OnGetLocationListener() {
                    @Override
                    public void onSuccess(RootLocation rootLocation) {
                        //       Log.e(TAG, "onSuccess: "+rootLocation);
                        setCenterLocationAndAddMaker(rootLocation.getData());
                    }

                    @Override
                    public void onError(String message, int code) {
                        Log.e(TAG, "onError: ");
                    }
                });
                locationUtil.getLocation();
            }
        }

    }
    /*
    * 设定中心点并添加Marker
    * */
    private void setCenterLocationAndAddMaker(LocationModel loc)
    {
        //设定中心点坐标
        LatLng cenpt = new LatLng(loc.getLatitude()
                ,loc.getLongtitude());
        //定义地图状态
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(cenpt)
                .zoom(16)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化

        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
        mBaiduMap.setMapStatus(mMapStatusUpdate);
        addMarker(cenpt.latitude,cenpt.longitude);
    }
    public void addMarker(double latitude,double longtitude)
    {
        //定义Maker坐标点
      final   LatLng point = new LatLng(latitude, longtitude);
//构建Marker图标
      //  BitmapDescriptor bitmap = BitmapDescriptorFactory
       //         .fromResource(R.mipmap.main_icon_figure);
        final CustomMaker maker = new CustomMaker(getContext(),null);
        maker.getTitle().setText(mVisitor.getName());
        AsyncImageLoader loader = new AsyncImageLoader(getContext());
        //将图片缓存至外部文件中
        loader.setCache2File(true); //false
        //设置外部缓存文件夹
        loader.setCachedDir(getContext().getCacheDir().getAbsolutePath());
        //下载图片，第二个参数是否缓存至内存中
        loader.downloadImage(mVisitor.getPortrait(), true, new AsyncImageLoader.ImageCallback()
        {
            @Override
            public void onImageLoaded(Bitmap bitmap, String imageUrl) {
                if(bitmap != null)
                {
                    maker.getImage().setImageBitmap(toRoundBitmap(bitmap));
                }
                BitmapDescriptor descriptor = BitmapDescriptorFactory.fromView(maker);

//构建MarkerOption，用于在地图上添加Marker
                OverlayOptions option = new MarkerOptions()
                        .position(point)
                        .icon(descriptor);
//在地图上添加Marker，并显示
                mBaiduMap.addOverlay(option);
            }
        });




    }
    /*
    * 设置为圆形图片
    * */
    public Bitmap toRoundBitmap(Bitmap bitmap){
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        int r=0;
        //取最短边做边长
        if(width<height){
            r=width;
        }else{
            r=height;
        }
        //构建一个bitmap
        Bitmap backgroundBm=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        //new一个Canvas，在backgroundBmp上画图
        Canvas canvas=new Canvas(backgroundBm);
        Paint p=new Paint();
        //设置边缘光滑，去掉锯齿
        p.setAntiAlias(true);
        RectF rect=new RectF(0, 0, r, r);
        //通过制定的rect画一个圆角矩形，当圆角X轴方向的半径等于Y轴方向的半径时，
        //且都等于r/2时，画出来的圆角矩形就是圆形
        canvas.drawRoundRect(rect, r/2, r/2, p);
        //设置当两个图形相交时的模式，SRC_IN为取SRC图形相交的部分，多余的将被去掉
        p.setXfermode(new PorterDuffXfermode(  PorterDuff.Mode.SRC_IN));
        //canvas将bitmap画在backgroundBmp上
        canvas.drawBitmap(bitmap, null, rect, p);
        return backgroundBm;
    }
}
